﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using Distribution.Service;
using Entities;

namespace DistributionApplication
{
    public partial class frmInspectionSettings : frmBase
    {
        #region - Constructor -

        public frmInspectionSettings()
        {
            InitializeComponent();
        }

        #endregion

        #region - Event handlers -

        private void frmInspectionSettings_Load(object sender, EventArgs e)
        {
            lblTitle.Text = "PPI | Settings";
            LButtonText = "Save";
            RButtonText = "Cancel";

            LoadCombos();

            foreach (var item in ddlInspector.Items)
            {
                if (((EUser)item).Id == Settings.User.Value)
                {
                    ddlInspector.SelectedItem = item;
                    break;
                }
            }
            //ddlInspector.SelectedItem = Settings.User;
        }

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            if (!ValidateResults())
                return;
            
            Settings.UtilityEntity = (ELookUp)ddlCompany.SelectedItem;
            Settings.UserEntity = (EUser)ddlInspector.SelectedItem;
            Settings.ProjectEntity = (EProject)ddlProject.SelectedItem;
            Settings.VendorEntity = (EVendor)ddlVendor.SelectedItem;
            Settings.ReceptionMode.Value = ddlMode.SelectedIndex;

            LSetting.Save(Settings);
            m_GPS.ChangeToMode((ReceptionMode)Settings.ReceptionMode.Value);
            lblRButton_Click(this, EventArgs.Empty);
        }

        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlVendor.SelectedIndex >= 0)
            {
                ddlProject.Enabled = true;

                List<EProject> projects = LProject.GetAll(ddlVendor.SelectedItem as EVendor);
                MobileUIHelper.FillDropdown(ddlProject, projects.ToArray(), null);

                List<EUser> inspectors = LUser.GetAll(ddlVendor.SelectedItem as EVendor);
                MobileUIHelper.FillDropdown(ddlInspector, inspectors.ToArray(), null);
            }
            else
                ddlProject.Enabled = false;
        }

        #endregion

        #region - Private methods -

        private void LoadCombos()
        {
            List<ELookUp> utilities = LLookUpService.GetAll(LookUpType.UtilityName);
            MobileUIHelper.FillDropdown(ddlCompany, utilities.ToArray(), null);

            List<EVendor> vendors = LVendor.GetAll();
            MobileUIHelper.FillDropdown(ddlVendor, vendors.ToArray(), null);

            List<EProject> projects = LProject.GetAll(Settings.VendorEntity);
            MobileUIHelper.FillDropdown(ddlProject, projects.ToArray(), null);

            ddlMode.Items.Clear();
            ddlMode.Items.Add("A Mode");
            ddlMode.Items.Add("B Mode");

            ddlMode.SelectedIndex = Settings.ReceptionMode.Value;

            ddlVendor.SelectedItem = Settings.VendorEntity;
            ddlProject.SelectedItem = Settings.ProjectEntity;
            ddlCompany.SelectedItem = Settings.UtilityEntity;
        }

        private bool ValidateResults()
        {
            bool _isValid = true;
            StringBuilder _Message = new StringBuilder();

            if (ddlCompany.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Company");
            }

            if (ddlInspector.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Inspector");
            }

            if (ddlVendor.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Vendor");
            }

            if (ddlProject.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Project");
            }

            if (!_isValid)
                MessageBox.Show(_Message.ToString());

            return _isValid;
        }

        private void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddlInspector.Items.Clear();
            //if (ddlCompany.SelectedIndex >= 0)
            //{
            //    List<ELookUp> inspectors = LLookUpService.GetAll(LookUpType.Inspector);
            //    ELookUp selectedCompany = (ELookUp) ddlCompany.SelectedItem;
            //    List<ELookUp> companyInspectors = inspectors.FindAll(delegate(ELookUp i) { return i.LookUp.Id == selectedCompany.Value; });
            //    MobileUIHelper.FillDropdown(ddlInspector, companyInspectors.ToArray(), null);
            //}
        }

        #endregion


    }
}